package nl.project.data.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.project.data.Parent;
import nl.project.data.Student;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

public class AppointmentProcess extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
	super.doGet(req, resp) ; 
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		DatastoreService d = DatastoreServiceFactory.getDatastoreService();

		Parent p = (Parent) req.getSession().getAttribute("user");
		Student st = (Student) req.getSession().getAttribute("student_Id");
				
		Key k = KeyFactory.createKey("Parents", "Parents");
		Query query = new Query("Parent", k).addFilter("parentId",
				FilterOperator.EQUAL, String.valueOf(p.getParentId()));
		Entity entity = d.prepare(query).asSingleEntity();
		
		Parent parent;
		
		try {
			parent = new Parent(p.getParentId());
		} catch (EntityNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		System.out.println(p.getParentId());
		
		
		for (int i = 0; i < 3; i++) {
			if (req.getAttribute("subject" + i) != null || req.getAttribute("subject" + i) != "") {
				System.out.println(entity.getKey());
				
				Entity e = new Entity("Enrollment", entity.getKey());
				Entity P = new Entity("Parent") ;
				try {
					 P = d.get(entity.getKey());
				} catch (EntityNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.setProperty("parentId", p.getParentId());
				Student student = (Student)req.getSession().getAttribute("student");
				e.setProperty("studentId", student.getStudentId());

				String code = req.getParameter(("subjects"));
				String[] codes = code.split(" ", 3);								
				
				System.out.println(codes[i]);
				e.setProperty("theacherCode", codes[i].trim());
				Query tQ = new Query("vak").addFilter("theacherCode", FilterOperator.EQUAL, codes[i].trim() );
				List<Entity> tl =  d.prepare(tQ).asList(FetchOptions.Builder.withDefaults());
				e.setProperty("theacherID", tl.get(0).getProperty("theacherID"));
				
				ArrayList<Boolean> s = new ArrayList<Boolean>();
				
			String days = req.getParameter("days");				
			boolean b1 = true;
				while(b1 ){
					if(days.lastIndexOf("voorkeur")> 0){
						s.add(true);
						s.add(true);
						s.add(true);
						break;
					}
					
					
					if(days.lastIndexOf("1") > 0){
						s.add(true);
					}else{s.add(false);}
					if(days.lastIndexOf("2") > 0 ){
						s.add(true);
					}else{s.add(false);}
					if(days.lastIndexOf("3") >  0){
						s.add(true);
					}else{s.add(false);}
					b1 =false ; 
				}
			
				e.setProperty("days", s);

				ArrayList<String> c = new ArrayList<String>();
				c.add((String) P.getProperty("emailAddress"));
				c.add((String) P.getProperty("addressLine")
						+ (String) P.getProperty("addressNumber"));
				c.add((String) P.getProperty("zipCode"));
				c.add((String) P.getProperty("residence"));
				c.add((String) P.getProperty("cellPhoneNumber"));
				e.setProperty("contact", c);
				
				
				String time = req.getParameter("pref");
				
				boolean b2 = true;
				while(b2 ){
					if(time.lastIndexOf("Vroeg") > 0){
						time="Early";
					}
					if(time.lastIndexOf("Laat") > 0){
						time="Later";
					}
					if(time.lastIndexOf("Geen")>0){
						time="None";
					}
					b2=false ;
				}
				e.setProperty("pref", time);
				
				try{
				d.put(e);
				System.out.println(i + "e gelukt");
				
				}catch(Exception ex) {
				System.out.println(i + "e niet gelukt");
				}
				
			}
		}
	//	check(req, entity.getKey());
		RequestDispatcher rd = req.getRequestDispatcher("/dashboard");
		rd.forward(req, resp);

	}
	
	public void check(HttpServletRequest req,  Key k){
		
		DatastoreService d = DatastoreServiceFactory.getDatastoreService();
		
		Query q = new Query("Enrollment", k); 
		List<Entity> l = d.prepare(q).asList(FetchOptions.Builder.withDefaults());
		
		for(Entity e : l ){
			System.out.println(e.toString());
			System.out.println(e.getProperty("studentId"));
			
			
		}
		
	}
	

}
